package lookandfeel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

public class colorScheme {
	//Create instance of colorscheme
	private static colorScheme lookAndFeel = new colorScheme();
	private UIDefaults userInterface;
	//Set default theme
	private String defaultTheme = "Dark Gray";
	private String selectedTheme;
	//Define colors
	private Color clrBackground;
	private Color clrForeground;
	private Color btnBackground;
	private Color btnHover;
	private Color btnBorderColor;
	private Color tpHover;
	private Color tblSelectedRow;
	private Color tblSelectedRowText;
	private Color mapBackground;
	private Color mnHover;
	
	public synchronized static colorScheme getColorScheme(){
		return lookAndFeel;
	}
	
	public String getSelectedTheme(){
		return selectedTheme;
	}
	//Getters and setters
	public Color getBoxBackground(){
		return clrBackground;
	}
	public Color getBtnBackground(){
		return btnBackground;
	}
	public Color getBtnHover(){
		return btnHover;
	}
	public Color getMapBackground(){
		return mapBackground;
	}
	public Color getMenuHover(){
		return mnHover;
	}

	//Constructor
	public colorScheme(){
		setTheme(defaultTheme);
	}
	//Set the theme
	public void setTheme(String theme){
		selectedTheme = theme;
		if(theme == "Red" ){
			clrBackground 		= new Color(70,32,32);
			clrForeground		= Color.white;
			btnBackground 		= new Color(98,45,45);
			btnHover			= new Color(112,112,112); //button
			btnBorderColor 		= new Color(59,27,27);
			tpHover				= new Color(197, 127, 127); //tabbedpane
			tblSelectedRow		= Color.black;
			tblSelectedRowText	= Color.white;
			mapBackground		= new Color(175, 200, 200);
			mnHover 			= new Color(145,145,145);
		}
		else if(theme == "Random" ){
			clrBackground 		= randomColor();
			clrForeground		= randomColor();
			btnBackground 		= randomColor();
			btnHover			= randomColor();
			btnBorderColor 		= randomColor();
			tpHover				= randomColor();
			tblSelectedRow		= randomColor();
			tblSelectedRowText	= randomColor();
			mapBackground		= randomColor();
			mnHover 			= randomColor();
		}
		else //Default theme = Dark gray
		{
			clrBackground 		= new Color(51,51,51);
			clrForeground		= Color.white;
			btnBackground 		= new Color(71,71,71);
			btnHover			= new Color(112,112,112); //button
			btnBorderColor 		= new Color(43,43,43);
			tpHover				= new Color(85, 85, 85); //tabbedpane
			tblSelectedRow		= new Color(28,28,28);
			tblSelectedRowText	= Color.white;
			mapBackground		= new Color(175, 200, 200);
			mnHover 			= new Color(85, 85, 85);
		}
		//Refresh the Colors
		refreshColors();
	}
	//Function randomColor
	public Color randomColor(){
		Random random = new Random();
		float r = random.nextFloat();
		float g = random.nextFloat();
		float b = random.nextFloat();;
		Color rColor = new Color(r,g,b);
		return rColor;
	}
	public void refreshColors(){
		//Get default uimanager
		userInterface = UIManager.getDefaults();
				
		//Define fonts
		Font btnFont = new Font("Dialog", Font.BOLD, 11);
		Font tblFont = new Font("Dialog", Font.BOLD, 12);
		
		//Set frame look and feel
		userInterface.put("Frame.background", clrBackground);
		userInterface.put("Frame.foreground", clrForeground);
		
		//Set container look and feel
		userInterface.put("Container.background", clrBackground);
		userInterface.put("Container.foreground", clrForeground);
		
		//Set panel look and feel
		userInterface.put("Panel.background", clrBackground);
		userInterface.put("Panel.foreground", clrForeground);
				
		//Set menu look and feel :: hover effect also for popupmenu
		userInterface.put("MenuBar.background", clrBackground);
		userInterface.put("Menu.background", clrBackground);
		userInterface.put("MenuItem.background", clrBackground);
		userInterface.put("MenuItem.foreground", clrForeground);
		userInterface.put("MenuItem.selectionBackground", mnHover);
		
		//Set button look and feel
		userInterface.put("Button.background", btnBackground);
		userInterface.put("Button.font", btnFont);
		userInterface.put("Button.foreground", clrForeground);
		userInterface.put("Button.border", new CompoundBorder(
											BorderFactory.createLineBorder(btnBorderColor),
											new EmptyBorder(3,15,3,15)));
		userInterface.put("Button.select", btnHover);

		//Set label look and feel
		userInterface.put("Label.background", clrBackground);
		userInterface.put("Label.foreground", clrForeground);
		
		//Set titled borders look and feel
		userInterface.put("TitledBorder.titleColor", clrForeground);
		userInterface.put("TitledBorder.border", BorderFactory.createLineBorder(clrForeground));
		
		//Set tabbedpane look and feel
		userInterface.put("TabbedPane.background", clrBackground);
		userInterface.put("TabbedPane.foreground", clrForeground);
		userInterface.put("TabbedPane.contentBorderInsets", new Insets(0, 1, 0, 0));
		userInterface.put("TabbedPane.selected", tpHover);
		
		//Set table look and feel
		userInterface.put("Table.background", clrBackground);
		userInterface.put("Table.foreground", clrForeground);
		userInterface.put("Table.gridColor", clrForeground);
		userInterface.put("Table.selectionBackground", tblSelectedRow);
		userInterface.put("Table.selectionForeground", tblSelectedRowText);
		userInterface.put("TableHeader.background", clrBackground);
		userInterface.put("TableHeader.foreground", clrForeground);
		userInterface.put("TableHeader.font", tblFont);
		userInterface.put("TableHeader.cellBorder", BorderFactory.createLineBorder(clrForeground, 1));
		userInterface.put("Table.focusSelectedCellHighlightBorder", BorderFactory.createEmptyBorder());
		
		//Set scrollpane look and feel
		userInterface.put("ScrollPane.background", clrBackground);
		userInterface.put("ScrollPane.border", BorderFactory.createLineBorder(clrForeground));
		userInterface.put("ScrollBar.background", clrBackground);
	}
}



